﻿using EMoney.IStock.Client.Models.MC;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.DataAccess.MC
{
    /// <summary>
    /// 个人信息数据访问类
    /// </summary>
    public class MyInfoDal
    {
        /// <summary>
        /// 新增个人信息
        /// </summary>
        /// <param name="myInfo"></param>
        /// <returns></returns>
        public int AddMyInfo(MC_MyInfo myInfo)
        {
            string sql = "insert into MC_MyInfo(UserID,iNum,Level) values(@UserID,@iNum,@Level)";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", SqlDbType.Int, myInfo.UserID);
            tools.AddSqlParameter("iNum", SqlDbType.Int, myInfo.iNum);
            tools.AddSqlParameter("Level", SqlDbType.Int, myInfo.Level);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 获取个人信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public MC_MyInfo GetMyInfo(int userID)
        {
            string sql = "select * from MC_MyInfo where UserID=@UserID";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, userID);

            return SQLHelper.GetDataByColumns<MC_MyInfo>(ConnectionManager.Emfstock, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 更新个人信息
        /// </summary>
        /// <param name="myInfo"></param>
        /// <returns></returns>
        public int UpdateMyInfo(MC_MyInfo myInfo)
        {
            string sql = "update MC_MyInfo set iNum=@iNum,Level=@Level,UpdateTime=@UpdateTime where UserID=@UserID";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("iNum", System.Data.SqlDbType.Int, myInfo.iNum);
            tools.AddSqlParameter("Level", System.Data.SqlDbType.Int, myInfo.Level);
            tools.AddSqlParameter("UpdateTime", System.Data.SqlDbType.DateTime, myInfo.UpdateTime);
            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, myInfo.UserID);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 用户信息增改查
        /// </summary>
        /// <param name="type"></param>
        /// <param name="userid"></param>
        /// <param name="username"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public DataTable UserMessage(string type, int userid, string username, string userpic, out string msg)
        {
            msg = string.Empty;
            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("userid", SqlDbType.Int, userid);
            tools.AddSqlParameter("type", SqlDbType.VarChar, type);
            tools.AddSqlParameter("username", SqlDbType.VarChar, username);
            tools.AddSqlParameter("userpic", SqlDbType.VarChar, userpic);
            tools.AddSqlParameter("msg", SqlDbType.VarChar, 20, msg, ParameterDirection.Output);

            DataTable dtResult = SQLHelper.ExecuteGetTable(ConnectionManager.Emfstock, CommandType.StoredProcedure, "p_web_bbs_useradd", tools.GetSqlParameters());

            msg = SQLTools.GetString(tools.GetSqlParameterByName("msg").Value);

            return dtResult;
        }

        /// <summary>
        /// 获取用户库用户信息
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public OneUserInfo GetOneUserInfo(string username)
        {
            //string msg = string.Empty;
            //string ret = string.Empty;

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("p_username", System.Data.SqlDbType.VarChar, username);

            tools.AddSqlParameter("p_msg", System.Data.SqlDbType.VarChar, 100, "", System.Data.ParameterDirection.Output);
            tools.AddSqlParameter("return", System.Data.SqlDbType.VarChar, System.Data.ParameterDirection.ReturnValue);

            List<OneUserInfo> list = SQLHelper.GetProcedureDataByColumnsLst<OneUserInfo>(ConnectionManager.Love_stock, "sp_qry_oneuserinfo", tools.GetSqlParameters());

            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }

            //msg = SQLTools.GetString(tools.GetSqlParameterByName("p_message").Value);
            //ret = SQLTools.GetInt(tools.GetSqlParameterByName("return").Value);
        }
    }
}
